using UnityEngine;
using System.Collections;

public class Harpoon : MonoBehaviour
{
	
	public int Health;
	public bool Invencible;
	public AudioClip HitSound;
	
	public Player Player;
	
	public float MinDistance;
	public float MaxDistance;
	
	public float MinVelocity;
	public float MaxVelocity;
	
	public bool Attack;
	
	void Update()
	{
		float distance = Vector3.Distance(
			Player.transform.position,
			transform.position
		);
		if (!Attack)
			if (distance <= MaxDistance)
			{
				rigidbody.velocity = new Vector3(
					MaxVelocity,
					0.0f,
					0.0f
				);
				Attack = true;
			}
			else if (distance <= MinDistance)
				rigidbody.velocity = new Vector3(
					MinVelocity,
					0.0f,
					0.0f
				);
	}
	
	void OnTriggerEnter(Collider collider)
	{
		switch (collider.name)
		{
		case "Side":
			rigidbody.velocity = Vector3.zero;
			rigidbody.constraints = RigidbodyConstraints.FreezePosition;
			break;
		case "Player":
			audio.PlayOneShot(HitSound);
			renderer.enabled = false;
			//gameObject.SetActive(false);
			break;
		case "Bullet":
			audio.PlayOneShot(HitSound);
			if (!Invencible)
			{
				Health--;
				if (Health <= 0)
				{
					renderer.enabled = false;
					//gameObject.SetActive(false);
				}
			}
			break;
		}
	}
	
}